﻿#include <iostream>
#include <stack>

using namespace std;

class Solution
{
public:
    // 844. 比较含退格的字符串
    bool backspaceCompare(string s, string t)
    {
        return Check(s) == Check(t);
    }

    string Check(string str)
    {
        string ret;
        for (auto ch : str)
        {
            if (ch != '#')
                ret += ch;
            else if (ret.size())
                ret.pop_back();
        }
        return ret;
    }

    // 227. 基本计算器 II
    int calculate(string s)
    {
        stack<int> st;
        char op = '+';
        int i = 0, n = s.size();
        while (i < n)
        {
            if (s[i] == ' ')
                i++;
            else if (s[i] >= '0' && s[i] <= '9')
            {
                int tmp = 0;
                while (i < n && s[i] >= '0' && s[i] <= '9')
                    tmp = tmp * 10 + (s[i++] - '0');    // 计算后需要移动i的位置
                if (op == '+')
                    st.push(tmp);
                else if (op == '-')
                    st.push(-tmp);
                else if (op == '*')
                    st.top() *= tmp;
                else
                    st.top() /= tmp;
            }
            else
            {
                op = s[i];
                i++;
            }
        }
        int ret = 0;
        while (!st.empty())
        {
            ret += st.top();
            st.pop();
        }
        return ret;
    }
};

int main()
{
    /*string s = "y#fo##f";
    string t = "y#f#o##f";
    cout << Solution().backspaceCompare(s, t);*/
    
    string c = "3+5*6-7+2";
    cout << Solution().calculate(c);

    return 0;
}